
#include <librally.h>

rp_rwlock_t lock;

void * t1(void *arg)
{
	Err err;
	
	err = rp_rwlock_rdlock(&lock);
	printf("Got a read lock ... \n");
	sleep(5);
	printf("Release the read lock ... \n");
	err = rp_rwlock_rdunlock(&lock);
	return NULL;
}

void * t2(void *arg)
{
	Err err;

	err = rp_rwlock_rdlock(&lock);
	printf("Got a read lock ... \n");
	sleep(5);
	printf("Release the read lock ... \n");
	err = rp_rwlock_rdunlock(&lock);
	return NULL;
}

void * t3(void *arg)
{
	Err err;

	printf("Try to get a write lock ... \n");
	err = rp_rwlock_wrlock(&lock);
	printf("Got a write lock ... \n");
	sleep(5);
	printf("Release the write lock ... \n");
	err = rp_rwlock_wrunlock(&lock);

	return NULL;
}

void * t4(void *arg)
{
	Err err;
  
	printf("Try to get a read lock ... \n");
	err = rp_rwlock_rdlock(&lock);
	printf("Got a read lock ... \n");
	sleep(5);
	printf("Release the read lock ... \n");
	err = rp_rwlock_rdunlock(&lock);
	return NULL;
}

int main()
{
	Err err;
	rp_thread_t tid[4];

	err = rp_rwlock_init(&lock);
	rp_thread_create(tid,  t1, NULL);
	rp_thread_create(tid+1, t2, NULL);
	rp_thread_create(tid+2, t3, NULL);
	rp_thread_create(tid+3, t4, NULL);

	sleep(20);
	err = rp_rwlock_destroy(&lock);
	return 0;
}

